package com.tenement.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.tenement.dao.TenementClientMapper;
import com.tenement.dao.TenementLogMapper;
import com.tenement.dao.TenementTypeMapper;
import com.tenement.domain.TenementClient;
import com.tenement.domain.TenementLog;
import com.tenement.domain.TenementUser;
import com.tenement.service.ClientService;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName : ClientServiceImpl  //类名
 * @Description : 客户服务接口实现类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-06 22:50  //时间
 */
@Service
public class ClientServiceImpl implements ClientService {

    /**
     * 客户dao
     */
    @Resource
    private TenementClientMapper clientMapper;

    /**
     * 日志dao
     */
    @Resource
    TenementLogMapper logMapper;

    /**
     * 收藏dao
     */
    @Resource
    private TenementTypeMapper typeMapper;

    /**
     * 查询所有(分页，条件)
     *
     * @param client    客户模型
     * @param page      当前页数
     * @param limit     显示行数
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<TenementClient> selectAll(TenementClient client, Integer page, Integer limit, String startDate, String endDate) {
        PageHelper.startPage(page, limit);
        return clientMapper.selectAll(null, client, startDate, endDate);
    }

    /**
     * 总行数
     *
     * @param client    客户模型
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public Long overCount(TenementClient client, String startDate, String endDate) {
        return clientMapper.overCount(null, client, startDate, endDate);
    }

    /**
     * id删除
     *
     * @param id 客户id
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int deleteById(String id, TenementUser user, String ip) {
        //houseClientMapper.deleteClient(id);

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("删除客户信息");
        TenementClient client=clientMapper.selectByPrimaryKey(id);
        StringBuffer logBody=new StringBuffer();
        Tools.logclientMod(client,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(2);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);
        return clientMapper.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids 客户id数组
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int deleteAll(String[] ids, TenementUser user, String ip) {

        List<String> list = new ArrayList<>(Arrays.asList(ids));
        List<TenementClient> clients=clientMapper.findAllId(list);
        StringBuffer logBody=new StringBuffer();
        Tools.logclientMods(clients,logBody);
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("批量删除客户信息");
        log.setLogBody(logBody.toString());
        log.setLogType(2);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);
        typeMapper.deleteClientAll(list);
        return clientMapper.updateAllPrimaryKey(list);
    }

    /**
     * 新增
     *
     * @param client 客户模型
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int add(TenementClient client, TenementUser user, String ip) {
        Integer overCount = clientMapper.overCountId();
        client.setClientIdentity(clientIdentity(overCount.toString()));
        client.setClientId(UUID.randomUUID().toString().replaceAll("-", ""));
        client.setRevision(1);
        client.setCreatedBy(user.getUserName());

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("新增客户信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logclientMod(client,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(1);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        int row=clientMapper.insertSelective(client);
        //client.setCreatedTime(new Date());
        if(row>0){
            return logMapper.insertSelective(log);
        }
        return 0;

    }

    private String clientIdentity(String clientIdentity) {

        int y = 6 - clientIdentity.length();
        String cid = "ID";
        for (int i = 0; i < y; i++) {
            cid += "0";
        }
        cid += clientIdentity;
        return cid;
    }

    /**
     * 修改
     *
     * @param client 客户模型
     * @param user
     * @param ip
     * @return
     */
    @Override
    public int update(TenementClient client, TenementUser user, String ip) {
        client.setUpdatedBy(user.getUserName());
        client.setUpdatedTime(new Date());
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(4);
        log.setLogTitle("修改客户信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logclientMod(client,logBody);
        log.setLogBody(logBody.toString());
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        int row= clientMapper.updateByPrimaryKeySelective(client);
        if(row>0){
            return logMapper.insertSelective(log);
        }

        return 0;
    }

    /**
     * id查询
     *
     * @param id
     * @return
     */
    @Override
    public TenementClient selectById(String id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TenementClient> findAll() {
        return clientMapper.findAll();
    }

    @Override
    public TenementClient findById(String id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public int findPhone(String clientPhone,String clientId) {
        return clientMapper.findPhone(clientPhone,clientId);
    }
}
